<template>
  <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px" class="demo-ruleForm login-container">
    <h3 class="title">{{$t('message.sys-login')}}</h3>
    <el-form-item prop="account">
      <el-input type="text" ref="userName" v-model="ruleForm2.account" auto-complete="off" :placeholder="$t('message.user')"></el-input>
    </el-form-item>
    <el-form-item prop="checkPass">
      <el-input type="password" ref="passWord" v-model="ruleForm2.checkPass" auto-complete="off" :placeholder="$t('message.pwd')"></el-input>
    </el-form-item>
    <el-form-item prop="yzm" class="yzm">
      <el-input type="text" v-model="ruleForm2.yzm" auto-complete="off" placeholder="请输入验证码"></el-input>
      <p v-if="isTimer===0" @click="sendCode">获取验证码</p>
      <div v-else class="time-loading">
        <span>{{time}}</span> s
      </div>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:100%;" @click.native.prevent="handleSubmit2" :loading="logining">{{$t('message.login')}}</el-button>
      <!--<el-button @click.native.prevent="handleReset2">重置</el-button>-->
    </el-form-item>
  </el-form>
</template>

<script>
// import { requestLogin } from '../api/api'
import url from '../common/js/http'
import axios from 'axios'
export default {
  data () {
    return {
      logining: false,
      ruleForm2: {
        account: '',
        checkPass: '',
        yzm: ''
      },
      rules2: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
          // { validator: validaePass }
        ],
        checkPass: [
          { required: true, message: '请输入密码', trigger: 'blur' }
          // { validator: validaePass2 }
        ],
        yzm: [
          { required: true, message: '请输入验证码', trigger: 'blur' }
          // { validator: validaePass2 }
        ]
      },
      isTimer: 0,
      time: 60
    }
  },
  computed: {
    userLevel () {
      return localStorage.getItem('userLevel') + ''
    }
  },
  methods: {
    handleReset2 () {
      this.$refs.ruleForm2.resetFields()
    },
    handleSubmit2 (ev) {
      this.$refs.ruleForm2.validate((valid) => {
        if (valid) {
          this.logining = true
          let loginParams = { userName: this.ruleForm2.account, passWord: this.ruleForm2.checkPass, authCode: Number(this.ruleForm2.yzm) }
          axios.post(`${url}/login`, this.toQueryString(loginParams)).then(res => {
            this.logining = false
            console.log(res)
            if (res.data.code !== 0) {
              this.$message({
                message: res.data.error,
                type: 'error'
              })
            } else {
              console.log('登陆成功')
              localStorage.setItem('user', loginParams.userName)
              localStorage.setItem('token', res.data.token)
              localStorage.setItem('userLevel', res.data.user_level)
              if (this.userLevel === '4') {
                this.$router.replace('/changeInEarn')
              } else {
                this.$router.replace('/Home')
              }
            }
          }).catch(() => {
            this.$message.error('登录失败')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    cleanArray (actual) {
      const newArray = []
      for (let i = 0; i < actual.length; i++) {
        if (actual[i]) {
          newArray.push(actual[i])
        }
      }
      return newArray
    },
    toQueryString (obj) {
      if (!obj) return ''
      return this.cleanArray(
        Object.keys(obj).map(key => {
          if (obj[key] === undefined) return ''
          return encodeURIComponent(key) + '=' + encodeURIComponent(obj[key])
        })
      ).join('&')
    },
    sendCode () {
      this.isTimer = 1
      if (this.ruleForm2.account === '') {
        this.$refs.userName.focus()
      } else if (this.ruleForm2.account === '') {
        this.$refs.passWord.focus()
      } else {
        let loginParams = { userName: this.ruleForm2.account, passWord: this.ruleForm2.checkPass }
        axios.post(`${url}/sentCode`, this.toQueryString(loginParams)).then((res) => {
          if (res.data.code !== 0) {
            this.$message({
              message: res.data.error,
              type: 'error'
            })
            return
          }
          let timer = setInterval(() => {
            this.time--
            if (this.time <= 0) {
              this.isTimer = 0
              this.time = 60
              clearInterval(timer)
            }
          }, 1000)
        })
      }
    }
  }
}

</script>

<style lang="scss" scoped>
  .login-container {
    /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 180px auto;
    width: 350px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;
    .title {
      margin: 0px auto 40px auto;
      text-align: center;
      color: #505458;
    }
    .remember {
      margin: 0px 0px 35px 0px;
    }
    .yzm {
      position: relative;
      p {
        position: absolute;
        right: 10px;
        top: -13px;
        height: 40px;
        line-height: 40px;
        color: #409EFF;
        cursor: pointer;
        opacity: .8;
        &:hover {
          opacity: 1;
        }
      }
      .time-loading {
        position: absolute;
        right: 0;
        top: 0;
        text-align: center;
        height: 40px;
        line-height: 40px;
        width: 50px;
        color: #fff;
        background: #dcdfe6;
        border-radius: 0 4px 4px 0;
      }
    }
  }
</style>
